package com.zjp.basetypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

import java.util.List;
@Component
public class RedisPipeline {
    @Autowired
    JedisPool jedisPool;


    public List<Object> plGet(List<String> keys) {


        try (Jedis resource = jedisPool.getResource()) {

            Pipeline pipelined = resource.pipelined();

            for (String key : keys) {
                pipelined.get(key);
            }
            return pipelined.syncAndReturnAll();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }


    public void plSet(List<String> keys,List<String> values) {
        if(keys.size()!=values.size()) {
            throw new RuntimeException("key和value个数不匹配！");
        }
        try (Jedis jedis = jedisPool.getResource()) {

            Pipeline pipelined = jedis.pipelined();
            for (int i = 0; i < keys.size(); i++) {
                pipelined.set(keys.get(i), values.get(i));
            }
            pipelined.sync();
        } catch (Exception e) {
            throw new RuntimeException("执行Pipeline设值失败！", e);
        }
    }
}
